var express = require('express'),
    wx = require('../base/wx'),
    mysql = require('mysql'),
    // sha1 = require('node-sha1'),
    // swig = require('swig-templates'),
    router = express.Router();
    // request = require('request');

router.get('/', function(req, res, next) {
    res.status(200).send('<h1>hello world</h1>');
    return;
    wx.goAccessToken(req, res, function () {
        res.redirect("/home");
    });
});

/*
router.get('/send1', function(req, res, next) {
    res.status(404).send();
});
router.get('/send2', function(req, res, next) {
    res.status(404).send('<h1>xxx</h1>');
});
router.get('/send3', function(req, res, next) {
    res.status(404).send({id:1,age:2});
});
router.get('/send4', function(req, res, next) {
    res.status(404).send(new Buffer('<h1>some html</h1>'));
});
router.get('/send5', function(req, res, next) {
    res.set('Content-Type', 'text/html');
    res.status(404).send(new Buffer('<h1>some html</h1>'));
});

router.get('/end1', function(req, res, next) {
    res.status(404).end();
});
router.get('/end2', function(req, res, next) {
    res.status(404).end('<h1>xxx</h1>');
});
router.get('/end3', function(req, res,next) {
    res.status(404).end({id:1,age:2});
});
router.get('/end4', function(req, res, next) {
    res.status(404).end(new Buffer('<h1>some html</h1>'));
});
*/

/*
router.post('/testbody', function(req, res, next) {
    kakarotto.logger.loog(req.get('Content-Type'));
    kakarotto.logger.loog(req.body);

    res.status(200).end('ok');
});


router.get('/file1', function(req, res, next) {
    res.render('page/file1');
});
router.get('/file2', function(req, res, next) {
    res.render('page/file2');
});
router.get('/file3', function(req, res, next) {
    res.render('page/file3');
});


router.post('/file1', function(req, res, next) {
    kakarotto.logger.loog(req.get('Content-Type'));
    kakarotto.logger.loog(req.body);
    kakarotto.logger.loog(req.file);
    kakarotto.logger.loog(req.files);
    res.status(200).end('ok');
});
router.post('/file2', function(req, res, next) {
    kakarotto.logger.loog(req.get('Content-Type'));
    kakarotto.logger.loog(req.body);
    kakarotto.logger.loog(req.file);
    kakarotto.logger.loog(req.files);
    res.status(200).end('ok');
});
router.post('/file3', function(req, res, next) {
    kakarotto.logger.loog(req.get('Content-Type'));
    kakarotto.logger.loog(req.body);
    kakarotto.logger.loog(req.file);
    kakarotto.logger.loog(req.files);
    res.status(200).end('ok');
});
*/



router.get('/home', function(req, res, next) {
    var result = {
            "code": 200,
            "message": [{
                "type": "mobile",
                "data": [{
                    "nav_id": 1,
                    "name": "iPhone48元",
                    "node_url": "/uploads/brand/20170724/23b2d6b9c04d139c1c7f4a64cdc57ba6.jpg"
                }, {
                    "nav_id": 2,
                    "name": "HUAWEI48元",
                    "node_url": "/uploads/brand/20170724/4aa2f02d5989a2fa7fafbdf9677a6faf.jpg"
                }, {
                    "nav_id": 3,
                    "name": "OPPO48元",
                    "node_url": "/uploads/brand/20170724/7d04aa812f7f3bede31e5c373db5ed39.jpg"
                }, {
                    "nav_id": 1,
                    "name": "iPhone48元",
                    "node_url": "/uploads/brand/20170724/23b2d6b9c04d139c1c7f4a64cdc57ba6.jpg"
                }, {
                    "nav_id": 2,
                    "name": "HUAWEI48元",
                    "node_url": "/uploads/brand/20170724/4aa2f02d5989a2fa7fafbdf9677a6faf.jpg"
                }, {
                    "nav_id": 3,
                    "name": "OPPO48元",
                    "node_url": "/uploads/brand/20170724/7d04aa812f7f3bede31e5c373db5ed39.jpg"
                }, {
                    "nav_id": 1,
                    "name": "iPhone48元",
                    "node_url": "/uploads/brand/20170724/23b2d6b9c04d139c1c7f4a64cdc57ba6.jpg"
                }, {
                    "nav_id": 2,
                    "name": "HUAWEI48元",
                    "node_url": "/uploads/brand/20170724/4aa2f02d5989a2fa7fafbdf9677a6faf.jpg"
                }, {
                    "nav_id": 3,
                    "name": "OPPO48元",
                    "node_url": "/uploads/brand/20170724/7d04aa812f7f3bede31e5c373db5ed39.jpg"
                }, {
                    "nav_id": 1,
                    "name": "iPhone48元",
                    "node_url": "/uploads/brand/20170724/23b2d6b9c04d139c1c7f4a64cdc57ba6.jpg"
                }, {
                    "nav_id": 2,
                    "name": "HUAWEI48元",
                    "node_url": "/uploads/brand/20170724/4aa2f02d5989a2fa7fafbdf9677a6faf.jpg"
                }, {
                    "nav_id": 3,
                    "name": "OPPO48元",
                    "node_url": "/uploads/brand/20170724/7d04aa812f7f3bede31e5c373db5ed39.jpg"
                }]
            }, {
                "type": "cup",
                "data": [{
                    "cup_id": 4,
                    "name": "18oz吸管杯79.9元系列",
                    "node_url": "/uploads/cup/20180130/5848c46ea9436f0e0c40ee60a7dbe28e.jpg",
                    "origin_price": "79.90"
                }, {
                    "cup_id": 6,
                    "name": "22oz吸管杯79.9元系列",
                    "node_url": "/uploads/cup/20180124/ca142f6d21d5df45e925528574d022f8.jpg",
                    "origin_price": "79.90"
                }, {
                    "cup_id": 7,
                    "name": "16oz吸管杯79.9元系列",
                    "node_url": "/uploads/cup/20180130/99cd95996bb0cc420264836562c48725.jpg",
                    "origin_price": "79.90"
                }, {
                    "cup_id": 8,
                    "name": "18oz-530ml星球杯79.9元系列",
                    "node_url": "/uploads/cup/20190528/85f6a329253d81e44db40ea7c3c28adb.jpg",
                    "origin_price": "79.90"
                }]
            }, {
                "type": "bag",
                "data": [{
                    "bag_id": 5,
                    "name": "横款无拉链",
                    "node_url": "/uploads/bag/20180308/d097279c033b9b79da49936845e00e18.jpg",
                    "origin_price": "68.00"
                }, {
                    "bag_id": 7,
                    "name": "竖版两用",
                    "node_url": "/uploads/bag/20180308/de0d1cf034eefecd00c125d6c1f55b53.jpg",
                    "origin_price": "68.00"
                }, {
                    "bag_id": 8,
                    "name": "竖版飘带",
                    "node_url": "/uploads/bag/20180308/06c9fee8070d9bbb8f0b00e0c6765eba.jpg",
                    "origin_price": "68.00"
                }]
            }],
            "imsg": "获取数据成功."
        };

    var titleObject = {
        mobile: '手机壳',
        cup: '杯子',
        bag: '包',
    };
    
    for (let item of result.message) {
        item.type_cn = titleObject[item.type];
        item.list = item.data;
        delete item.data;
    }
    // 已经该变了 result.message;

    res.render('page/home', {
        stupid_data: result.message
    });
    return;




    // if(parseInt(req.cookies.user_id) > 0) {
    kakarotto.request.get({
        url: kakarotto.config.api.origin + '/index/index/goods?user_id=' + req.cookies.user_id
    }, function (error, response, body) {
        try {
            var result = JSON.parse(body);
            if (result.code == 200) {
                res.render('page/home', {
                    message: result.message,
                    // apiOrigin: kakarotto.config.api.origin
                });
            } else {
                res.render('page/home', {
                    message: '请求数据失败'
                });
            }
        } catch (e) {
            var err = new Error('Not Found');
            err.status = 500;
            next(err);
            return;
        }
    });
    // } else {
    //     res.cookie('user_id', false, {
    //         maxAge: config.cookieTime,
    //         httpOnly: false
    //     });
    //     res.redirect("/");
    // }
});


//创建连接
// var connection = mysql.createConnection({
//     host     : 'localhost',
//     user     : 'root',
//     password : '123456',
//     database : 'fortest'
// });
//执行创建连接 
// connection.connect();

/*
router.get('/mysql-select', function(req, res, next) {
    kakarotto.config.app.connection.connect();
    kakarotto.config.app.connection.query("SELECT * FROM express_test", function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ', err.message);
          return;
        }
 
       console.log('--------------------------SELECT----------------------------');
       console.log(result);
       console.log('------------------------------------------------------------\n\n');  
    });
     
    kakarotto.config.app.connection.end();

    res.status(200).end('<h1>/mysql-select</h1>');
});
*/

// 旋转自定义图片时新增表数据
router.get('/mysql-insert', function(req, res, next) {
    // 必须每次都新建，才能反复调用；否则只能执行一次，第二次就直接报错了。
    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : '123456',
        database : 'fortest'
    });
    connection.connect();
    // connection.query("INSERT INTO express_test (title,content) VALUES ('1', '2')", function (err, result) {
    connection.query("INSERT INTO express_test (title,content) VALUES ('"+ req.query.title +"', '"+ req.query.content +"')", function (err, result) {
        if(err){
          console.log('[INSERT ERROR] - ', err.message);
          return;
        }
 
       console.log('--------------------------INSERT----------------------------');
       console.log(result);
       console.log('------------------------------------------------------------\n\n');  
    });
     
    connection.end();

    // res.status(200).end('<h1>/mysql-insert</h1>');
    res.status(200).send({errcode: 200, message: 'Ok'});
});

module.exports = router;
